import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainFrame extends JFrame {

	private JPanel contentPane;
	private JTextArea txtResult;
	private BiTree tree;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 552, 324);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(14, 93, 506, 166);
		contentPane.add(scrollPane);

		txtResult = new JTextArea();
		txtResult.setText("choose an Traverse methond.");
		scrollPane.setViewportView(txtResult);

		JLabel lblTraverse = new JLabel("Traverse:");
		lblTraverse.setBounds(14, 16, 72, 18);
		contentPane.add(lblTraverse);

		JButton btnPre = new JButton("preRoot");
		btnPre.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnPreCall();
			}
		});
		btnPre.setBounds(48, 50, 113, 27);
		contentPane.add(btnPre);

		JButton btnIn = new JButton("inRoot");
		btnIn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnInCall();
			}
		});
		btnIn.setBounds(209, 50, 113, 27);
		contentPane.add(btnIn);

		JButton btnPost = new JButton("postRoot");
		btnPost.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnPostCall();
			}
		});
		btnPost.setBounds(370, 50, 113, 27);
		contentPane.add(btnPost);

		// ---------------------------------------------
		BiTreeNode g = new BiTreeNode("G");
		BiTreeNode e = new BiTreeNode("E", g, null);
		BiTreeNode d = new BiTreeNode("D");
		BiTreeNode b = new BiTreeNode("B", d, e);
		BiTreeNode h = new BiTreeNode("H");
		BiTreeNode f = new BiTreeNode("F", null, h);
		BiTreeNode c = new BiTreeNode("C", f, null);
		BiTreeNode a = new BiTreeNode("A", b, c);
		tree = new BiTree(a);
	}

	protected void btnPostCall() {
		String string = tree.postRootTraverse(tree.getRoot());
		txtResult.setText(string);
	}

	protected void btnInCall() {
		String string = tree.inRootTraverse(tree.getRoot());
		txtResult.setText(string);
	}

	protected void btnPreCall() {
		String string = tree.preRootTraverse(tree.getRoot());
		txtResult.setText(string);
	}
}
